package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.auth.api.model.UserInfo;
import com.sdy.auth.client.service.SsoService;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.supermarket.biz.service.GsNoticeService;
import com.sdy.supermarket.biz.model.GsNotice;
import com.sdy.common.model.Response;
import com.sdy.supermarket.biz.service.GsUserNoticeService;
import com.sdy.supermarket.biz.utils.PageUtils;
import com.sdy.usercenter.api.dto.ResourceDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 韩亚恒
 * @since 2019-07-23
 */
@Slf4j
@RestController
@RequestMapping("/gsNotice")
public class GsNoticeController extends BaseController {
    @Autowired
    private GsNoticeService gsNoticeService;

    @Autowired
    private SsoService ssoService;

    @Autowired
    private GsUserNoticeService gsUserNoticeService;

    /**
     * 分页查询(条件可为空)
     *
     * @param noticeName   通知名称
     * @param noticeText   通知详情
     * @param noticeStatus 通知状态
     * @param page         (size current)
     * @return
     * @throws BizException
     */
    @GetMapping("/getList")
    public Response pageListGsNotice(String noticeName, String noticeText, Integer noticeStatus, Page<GsNotice> page) throws UnsupportedEncodingException {
        if (null != noticeName) {
            noticeName = URLDecoder.decode(noticeName, "UTF-8");
        }
        if (null != noticeText) {
            noticeText = URLDecoder.decode(noticeText, "UTF-8");
        }

        IPage<GsNotice> gsNoticeIPage = gsNoticeService.pageListGsNotice(noticeName, noticeText, noticeStatus, page);
        return Response.success(gsNoticeIPage);
    }

    /**
     * 新增和修改方法,如果传入ID则执行修改，否则执行新增
     *
     * @param gsNotice
     * @return
     */
    @PostMapping("/saveOrUpdateNotice")
    @Transactional(transactionManager = "transactionManager", propagation = Propagation.REQUIRED)
    public Response saveNotice(@RequestBody GsNotice gsNotice) throws BizException {
        int i = gsNoticeService.saveOrUpdateGsNotice(gsNotice);
        return i != 0 ? Response.success("操作成功") : Response.error("操作失败");
    }

    /**
     * 根据ID删除Notice
     *
     * @param notice
     * @return
     */
    @PostMapping("/deleteById")
    public Response deleteNoticeById(@RequestBody GsNotice notice) throws BizException {
        int i = gsNoticeService.deleteGsNoticeById(notice.getNoticeId());
        return i != 0 ? Response.success("删除成功") : Response.error("删除失败");
    }

    /**
     * 根据ID获取单条数据
     *
     * @param noticeId
     * @return
     */
    @GetMapping("/getNoticeById")
    @Transactional(transactionManager = "transactionManager", propagation = Propagation.REQUIRED)
    public Response getNoticeById(Integer noticeId, HttpServletRequest request) throws BizException {
        UserInfo userInfo = ssoService.getUserInfo(request);
        GsNotice byId = gsNoticeService.getById(noticeId);
        List<ResourceDTO> appList = gsNoticeService.getAppList();
        appList.forEach(app -> {
            if (app.getId().equals(byId.getNoticePlatformId())) {
                byId.setNoticePlatformName(app.getName());
            }
        });
        int read = gsUserNoticeService.noticeHaveRead(userInfo.getUserId(), byId.getNoticeId());
        if (read == 0) {
            throw new BizException("获取数据失败");
        }
        return Response.success(byId);
    }

    /**
     * 获取应用列表
     *
     * @return
     */
    @GetMapping("/getAppList")
    public Response getAppList(Page<GsNotice> page) {
        List<ResourceDTO> appList = gsNoticeService.getAppList();
        return Response.success(appList);
    }
}
